package org.wddsicmd.util;
import org.wddsicmd.util.Log;

import java.io.BufferedInputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.Properties; 



public class CustomProperties { 

    final static String TAG="CustomProperties"; 

    public final static String USER_DIR_DEFAULT = "MSS/USER";
    public final static String PROPERTIES_DIR_DEFAULT = "MSS/CFG";
    public final static String TEMP_DIR_DEFAULT = "MSS/TEMP";
    public final static String PROPERTIES_FILE_DEFAULT = "MSSCFG.properties";

    String mPropsertiesDir = PROPERTIES_DIR_DEFAULT;

	
    String profilepath=PROPERTIES_FILE_DEFAULT; 

    private Properties props = new Properties(); 
/* doom-2015-10-29 11:54:41 
    static { S
        try { 		
//		props.load(CustomProperties.class.getClassLoader().getResourceAsStream(profilepath));
            props.load(new FileInputStream(profilepath)); 
        } catch (FileNotFoundException e) { 
            e.printStackTrace(); 
            System.exit(-1); 
        } catch (IOException e) {        
            System.exit(-1); 
        } 
    } 
 doom-2015-10-29 11:54:41 */
	public CustomProperties(String dirName, String fileName){
		init(USER_DIR_DEFAULT, "");
		init(TEMP_DIR_DEFAULT, "");
		init(PROPERTIES_DIR_DEFAULT, "");
		
		init(dirName, fileName);
      }

	public CustomProperties(){
		
/* doom-2015-10-29 11:39:40 
		String filePath = this.getClass().getResource(profilepath).getFile();
		Log.d(TAG, "CustomProperties filePath:"+filePath);
 doom-2015-10-29 11:39:40 */
		init(USER_DIR_DEFAULT, "");
		init(TEMP_DIR_DEFAULT, "");
		init(PROPERTIES_DIR_DEFAULT, "");

		init(PROPERTIES_DIR_DEFAULT, PROPERTIES_FILE_DEFAULT);
	}

	private void init(String dirName, String fileName){

		mPropsertiesDir = dirName;
		profilepath = fileName;
		
		String rootDir = getConfigPropertiesRootDir();

		File rootDirFile = new File(rootDir);
		if(!rootDirFile.exists()){
			rootDirFile.mkdirs();
		}
		profilepath = getConfigPropertiesFilePath(profilepath);		
		File proFile = new File(profilepath);
		if(!proFile.exists()){
			try {
				proFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

       public String getConfigPropertiesRootDir(){
		String osStr = System.getProperties().getProperty("os.name");
		if(osStr != null && "Linux".equals(osStr)){
			return System.getProperties().getProperty("user.home") + "/"+mPropsertiesDir;
		}else{
			return "C:\\"+mPropsertiesDir;
		}
       }

       public String getConfigPropertiesFilePath(String fileName){
		String osStr = System.getProperties().getProperty("os.name");
		if(osStr != null && "Linux".equals(osStr)){
			return System.getProperties().getProperty("user.home") + "/"+mPropsertiesDir+"/"+fileName;
		}else{
			return "C:\\"+mPropsertiesDir+"\\"+fileName;
		}
       }

       public static String getHomePath(){
		String osStr = System.getProperties().getProperty("os.name");
		if(osStr != null && "Linux".equals(osStr)){
			return System.getProperties().getProperty("user.home");
		}else{
			return "C:\\";
		}
       }
       public static String getConfigPath(){
		String osStr = System.getProperties().getProperty("os.name");
		if(osStr != null && "Linux".equals(osStr)){
			return System.getProperties().getProperty("user.home") + "/"+PROPERTIES_DIR_DEFAULT;
		}else{
			return "C:\\"+PROPERTIES_DIR_DEFAULT;
		}
       }
       public static String getUserPath(){
		String osStr = System.getProperties().getProperty("os.name");
		if(osStr != null && "Linux".equals(osStr)){
			return System.getProperties().getProperty("user.home") + "/"+USER_DIR_DEFAULT;
		}else{
			return "C:\\"+USER_DIR_DEFAULT;
		}
       }
       public static String getTempPath(){
		String osStr = System.getProperties().getProperty("os.name");
		if(osStr != null && "Linux".equals(osStr)){
			return System.getProperties().getProperty("user.home") + "/"+TEMP_DIR_DEFAULT;
		}else{
			return "C:\\"+TEMP_DIR_DEFAULT;
		}
       }
	   
    public String getKeyValue(String key) { 
        return props.getProperty(key); 
    } 

    public String readValue(String key) { 
        Properties props = new Properties(); 
        try { 
		//InputStream in = CustomProperties.class.getClassLoader().getResourceAsStream(profilepath);

            InputStream in = new BufferedInputStream(new FileInputStream(profilepath)); 
            props.load(in); 
            String value = props.getProperty(key); 
            System.out.println(key +":"+ value); 
            return value; 
        } catch (Exception e) { 
            e.printStackTrace(); 
            return null; 
        } 
    } 

    public String readValue(String filePath, String key) { 
        Properties props = new Properties(); 
        try { 
		//InputStream in = CustomProperties.class.getClassLoader().getResourceAsStream(profilepath);

            InputStream in = new BufferedInputStream(new FileInputStream(filePath)); 
            props.load(in); 
            String value = props.getProperty(key); 
            System.out.println(key +":"+ value); 
            return value; 
        } catch (Exception e) { 
            e.printStackTrace(); 
            return null; 
        } 
    } 
    
    public void writeValue(String keyname,String keyvalue) {        
        try { 
            OutputStream fos = new FileOutputStream(profilepath); 
            props.setProperty(keyname, keyvalue); 
            props.store(fos, "Update '" + keyname + "' value"); 
        } catch (IOException e) { 
            System.err.println("update error"); 
        } 
    } 


    public void udpateValue(String keyname,String keyvalue) { 
        try { 
            props.load(new FileInputStream(profilepath)); 
            OutputStream fos = new FileOutputStream(profilepath);            
            props.setProperty(keyname, keyvalue); 
            props.store(fos, "Update '" + keyname + "' value"); 
        } catch (IOException e) { 
            System.err.println("update error"); 
        } 
    } 


        //readValue("mail.properties", "MAIL_SERVER_PASSWORD"); 
        //writeProperties("MAIL_SERVER_INCOMING", "327@qq.com");        

} 


